package com.easemob.chat.core;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Pair;
import com.easemob.analytics.EMLoginCollector;
import com.easemob.analytics.EMTimeTag;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatConfig;
import com.easemob.chat.EMChatConfig.EMEnvMode;
import com.easemob.cloud.HttpClientConfig;
import com.easemob.cloud.HttpClientManager;
import com.easemob.exceptions.EMAuthenticationException;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.CryptoUtils;
import com.easemob.util.EMLog;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class k
{
  private static final String d = k.class.getName();
  static int a = 443;
  private static final String e = "access_token";
  private String f = null;
  private String g = null;
  private String h = null;
  private long i = 0L;
  private boolean j = true;
  private static k k = null;
  private static final String l = "im1.easemob.com";
  private static final String m = "a1.easemob.com";
  private static final String n = "im1.vip1.easemob.com";
  private static final String o = "a1.vip1.easemob.com";
  private String p = "im1.easemob.com";
  private String q = "a1.easemob.com";
  private static final String r = "im1.sandbox.easemob.com";
  private static final String s = "a1.sdb.easemob.com";
  private String t = "im1.sandbox.easemob.com";
  private String u = "a1.sdb.easemob.com";
  private static final String v = "im1.dev.easemob.com";
  private static final String w = "a1.dev.easemob.com";
  private String x = "im1.dev.easemob.com";
  private String y = "a1.dev.easemob.com";
  private static String z = "203.195.185.236";
  private static int A = 3488;
  List<a> b = new ArrayList();
  private CryptoUtils B = null;
  EMChatConfig.EMEnvMode c = EMChatConfig.EMEnvMode.EMProductMode;
  private boolean C = false;
  private boolean D = false;
  private boolean E = false;
  private static final String F = "www.easemob.com";
  private static String[] G = { "Hy+xe9oDskKv5ZSkV4gLlCEW+t5gJOlzVd2oYYSJ9PY=", "6UJPCvc50DcJzJPQrh9GTxzLx7ExBUk/d/QSveCDBHA=", "IW07cwaTAhAm741v3TDuuvW/i8SGwkKPuxFbXhtyegk=", "MxmVut7Ui09MCvgOTcmgB+cDXhq+g0vPxG5Sz+OVkfI=", "Ok6j+A2TySWNmuZms7cji8eTdxYMoxuugbsghZT5Oss=", "f8K5HQ82hreMKpawCmtAikMcvoTfGm/pSPtHgwUvsPk=", "ZrJ41xgzvmNvhKDz7ZhTaRzLx7ExBUk/d/QSveCDBHA=" };
  private String H = "2.1.8";

  private k()
  {
    a locala = new a();
    locala.a = z;
    locala.b = A;
    this.b.add(locala);
    a(true);
    this.B = new CryptoUtils();
    this.B.initAES();
  }

  public static synchronized k a()
  {
    if (k == null)
      k = new k();
    return k;
  }

  public String b()
  {
    return this.H;
  }

  void a(boolean paramBoolean)
  {
    b(paramBoolean);
    c(paramBoolean);
    d(paramBoolean);
  }

  public void b(boolean paramBoolean)
  {
    this.C = paramBoolean;
  }

  public boolean c()
  {
    return this.C;
  }

  public void c(boolean paramBoolean)
  {
    this.D = paramBoolean;
  }

  public boolean d()
  {
    return this.D;
  }

  public void d(boolean paramBoolean)
  {
    this.E = paramBoolean;
  }

  public boolean e()
  {
    return this.E;
  }

  public String f()
  {
    if (g())
      return "im1.vip1.easemob.com";
    return this.p;
  }

  boolean g()
  {
    String[] arrayOfString;
    int i2 = (arrayOfString = G).length;
    int i1 = 0;
    while (i1 < i2)
    {
      String str = arrayOfString[i1];
      try
      {
        if (this.B.decryptBase64String(str).equals(EMChatConfig.getInstance().APPKEY))
          return true;
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        i1++;
      }
    }
    return false;
  }

  public String h()
  {
    if (g())
      return "a1.vip1.easemob.com";
    return this.q;
  }

  public String i()
  {
    return "www.easemob.com";
  }

  public void a(String paramString)
  {
    this.p = paramString;
  }

  public void b(String paramString)
  {
    this.q = paramString;
  }

  public String j()
  {
    return this.t;
  }

  public String k()
  {
    return this.u;
  }

  public String l()
  {
    return this.x;
  }

  public String m()
  {
    return this.y;
  }

  public String n()
  {
    return "im1.easemob.com";
  }

  public String o()
  {
    return "a1.easemob.com";
  }

  public void a(EMChatConfig.EMEnvMode paramEMEnvMode)
  {
    this.c = paramEMEnvMode;
    if (this.c == EMChatConfig.EMEnvMode.EMSandboxMode)
    {
      this.p = this.t;
      this.q = this.u;
    }
    else if (this.c == EMChatConfig.EMEnvMode.EMDevMode)
    {
      this.p = this.x;
      this.q = this.y;
    }
    else
    {
      this.p = "im1.easemob.com";
      this.q = "a1.easemob.com";
    }
  }

  public EMChatConfig.EMEnvMode p()
  {
    return this.c;
  }

  public synchronized String a(String paramString1, String paramString2)
    throws EaseMobException, EMAuthenticationException, JSONException, IOException
  {
    this.f = paramString1;
    this.g = paramString2;
    b localb = e.a().l(paramString1);
    long l1 = System.currentTimeMillis();
    if ((localb != null) && (localb.a() != null))
    {
      this.h = localb.a();
      this.i = localb.b();
    }
    else
    {
      this.h = null;
    }
    if ((this.h == null) || (l1 - this.i > 561600000L))
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append(HttpClientConfig.getBaseUrlByAppKey());
      localStringBuilder.append("/token");
      JSONObject localJSONObject1 = new JSONObject();
      try
      {
        localJSONObject1.put("grant_type", "password");
        localJSONObject1.put("username", paramString1);
        localJSONObject1.put("password", this.g);
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
      EMTimeTag localEMTimeTag = new EMTimeTag();
      localEMTimeTag.start();
      EMLog.d(d, "try to retrieve token : " + localStringBuilder.toString());
      Pair localPair = null;
      localPair = HttpClientManager.sendRequest(localStringBuilder.toString(), null, localJSONObject1.toString(), HttpClientManager.Method_POST);
      EMLoginCollector.collectRetrieveTokenTime(localEMTimeTag.stop());
      if (localPair == null)
        throw new EaseMobException(-999, "failed to retrieve token");
      if (((Integer)localPair.first).intValue() == 200)
      {
        if (TextUtils.isEmpty((CharSequence)localPair.second))
        {
          EMLog.d(d, "return code is ok, but content is empty!");
          throw new EaseMobException(-999, "failed to retrieve token, the content is empty!");
        }
        JSONObject localJSONObject2 = null;
        try
        {
          localJSONObject2 = new JSONObject((String)localPair.second);
          this.h = localJSONObject2.getString("access_token");
        }
        catch (Exception localException)
        {
          EMLog.d(d, "accesstoken:" + (String)localPair.second);
          throw new EaseMobException(-999, "wrong content is returned : " + (String)localPair.second);
        }
        this.i = System.currentTimeMillis();
        if (localb == null)
          localb = new b();
        localb.a(this.h).a(this.i);
        e.a().a(paramString1, localb);
        EMLog.d(d, "accesstoken : " + (String)localPair.second);
      }
      else
      {
        if ((((Integer)localPair.first).intValue() == 401) || (((Integer)localPair.first).intValue() == 400) || (((Integer)localPair.first).intValue() == 404))
          throw new EMAuthenticationException("invalid user or password!");
        throw new EaseMobException(-999, "failed to retrieve token with error code : " + localPair.first);
      }
    }
    return this.h;
  }

  public synchronized String q()
  {
    if ((this.f == null) || (this.g == null))
      return null;
    String str = null;
    try
    {
      str = a(this.f, this.g);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      EMLog.d(d, localException.getMessage());
    }
    return str;
  }

  public long r()
  {
    return this.i;
  }

  public void s()
  {
    b localb = new b();
    localb.a(0L);
    localb.a("");
    e.a().a(this.f, localb);
  }

  public void e(boolean paramBoolean)
  {
    this.j = paramBoolean;
  }

  public boolean t()
  {
    return this.j;
  }

  public String c(String paramString)
  {
    String str1 = "";
    try
    {
      InputStreamReader localInputStreamReader = new InputStreamReader(EMChat.getInstance().getAppContext().getResources().getAssets().open(paramString));
      BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
      String str2 = "";
      while ((str2 = localBufferedReader.readLine()) != null)
        str1 = str1 + str2;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return str1;
  }

  public void d(String paramString)
  {
    n.a().a(paramString);
  }

  public void e(String paramString)
  {
    n.a().b(paramString);
  }

  public void a(long paramLong)
  {
    n.a().a(paramLong);
  }

  public void b(long paramLong)
  {
    n.a().b(paramLong);
  }

  public long u()
  {
    return n.a().b();
  }

  public String v()
  {
    return n.a().c();
  }

  public String w()
  {
    return n.a().d();
  }

  public long x()
  {
    return n.a().e();
  }

  public String f(String paramString)
  {
    return HttpClientConfig.getFileRemoteUrl(paramString);
  }

  public String y()
  {
    return HttpClientConfig.getFileDirRemoteUrl();
  }

  public String z()
  {
    return HttpClientConfig.getBaseUrlByAppKey();
  }

  public void a(List<a> paramList)
  {
  }

  public List<a> A()
  {
    return this.b;
  }

  public boolean B()
  {
    f.c localc = f.a().c();
    return localc.c.equals("https");
  }

  public static class a
  {
    public String a = null;
    public int b = -1;
    public a c = a.a;

    public static enum a
    {
    }
  }

  public static class b
  {
    String a;
    long b;

    public b()
    {
    }

    public b(String paramString, long paramLong)
    {
      this.a = paramString;
      this.b = paramLong;
    }

    public String a()
    {
      if (this.b <= 0L)
        this.a = null;
      return this.a;
    }

    public b a(String paramString)
    {
      this.a = paramString;
      return this;
    }

    public long b()
    {
      return this.b;
    }

    public b a(long paramLong)
    {
      this.b = paramLong;
      return this;
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.core.k
 * JD-Core Version:    0.6.1
 */